import character from '../components/player/character'
import cord from '../components/npc/cord'
import CoinsBg from '../components/npc/coins-bg'
import Scoreboard from '../components/npc/scoreboard'
import Page from '../components/base/page'
import store from '../store/index'
import Music from '../store/music'
import { getCtx, getCanvas } from '../../ctx/index'
import { ROLE_STATE, SPCIES_TYPE } from '../constant/index'
import Event from "../event/index"
// import Bg from '../components/home/Bg'

let install = null

/**
 * 游戏主函数
 */
export default class Coins extends Page {
  constructor(ctx = null) {
    super(ctx)
    if (install) {
      install.restart()
      return install
    }
    this.restart()
    Event.on('restart', () => {
      store.reset()
      this.restart()
    })
    install = this
  }

  restart() {
    const ctx = getCtx()
    store.gameOver = false
    this.bg = new CoinsBg(ctx)
    this.scoreboard = new Scoreboard(ctx)
    this.character = new character(ctx)
    this.cord = new cord(ctx)
    this.appendChild(this.bg, this.scoreboard, this.character, this.cord)
    this.cord.clickCallBack(() => {
      this.character.setStateType(ROLE_STATE.INIT.type)
      this.cord.startMove()
    })
    this.addEvent('touchend', (e) => {
      this.cord.touchend(e)
      Music.createdAudio('gameClickBgm')
    })
    this.addEvent('touchmove', (e) => {
      // console.log(e)
    })
    this.$onShow(() => {
      Music.setAudio({
        name: 'GameBgm',
        play: true,
        autoplay: true,
        volume: 0.08
      })
      const loop = () => {
        Music.GameBgm.onEnded(() => {
          Music.updateMusic('GameBgm')
          Music.GameBgm.volume = 0.08
          loop()
        })
      }
      loop()
    })
    this.$onHide(() => {
      Music.pause('GameBgm')
    })
  }

  // 全局碰撞检测
  collisionDetection() {
    store.golds.forEach((gold) => {
      /**
       * 金币、pus，与地面碰撞
       */
      if (gold.isCollideWithGround(20) && !gold.suspend && gold.type === SPCIES_TYPE.BOMB.type) {
        gold.End()
        store.haveGolds.push(gold)
        // this.cord.startMove()
        Music.createdAudio('explosionSound')
      }
      /**
       * 检测金币是否与墙壁碰撞
       */
      gold.updateCollideWithWallDirection()

      /**
       * 检测金币是否触碰到地面
       */
      if (gold.isCollideWithGround() && !gold.suspend) {
        this.character.setStateType(ROLE_STATE.LOSE.type)
        gold.destroy()
        // this.cord.startMove()
      
      }
      /**
       * 打工人是否吃到金币
       */
      if (this.character.isCollideWith(gold) && !gold.suspend && gold.isFall) {
        gold.End()
        store.haveGolds.push(gold)
        this.character.createdBonusPoints(gold.money, gold.type)
        
        if (gold.type === SPCIES_TYPE.GOLD.type) {
        
          store.$updateScore(store.score + gold.money)
          this.character.setStateType(ROLE_STATE.RECEIVE.type)
          Music.createdAudio('goldDrops')
        
        } else if (gold.type === SPCIES_TYPE.BOMB.type) {
        
          this.character.setStateType(ROLE_STATE.BOMB.type)
          Music.createdAudio('explosionSound')
          Music.createdAudio('receivedBomb')
          store.gameOver = true
          Event.emit('gameover')
        
        }
        // this.cord.startMove()
      }

      /**
       * 金币是否掉出屏幕
       */
      if (gold.isFallscreen(50) && gold.isFall) {
        // this.cord.startMove()
        // gold.destroy()
      }

      /**
       * 动画是否结束
       */
      if (gold.isAnimationOver) {
        gold.destroy()
      }
    })
  }
  /**
   * canvas重绘函数
   * 每一帧重新绘制所有的需要展示的元素
   */
  render(ctx = getCtx()) {
    this.bg.render(ctx)
    this.scoreboard.render(ctx)
    this.character.render(ctx)
    this.cord.render(ctx)
    store.golds.forEach((item) => {
      if (item.suspend) {
        item.aniRender(ctx)
      } else {
        item.render(ctx)
      }
    })
    this.scoreboard.renderGameScore(ctx, store.score)
    this.collisionDetection()
  }

  // 游戏逻辑更新主函数
  update() {
    if (store.gameOver) return

    this.cord.update()
    this.character.update()
    store.golds.forEach((item) => item.update())
  }
}
